package view.component.render
{
import core.Language;
import core.model.PlayerModel;

import flash.display.MovieClip;

import framework.view.components.button.ButtonProxy;

import mx.utils.StringUtil;

import values.building.FinalBuildFriendCostPositionValue;
import values.item.INumbericItemValue;

import view.component.asset.IconProxy;

/**
 * SpecialFinalBuildItemRender
 * @author jliang
 *
 */
public class SpecialFinalBuildItemRender extends AssetItemRenderProxy
{
	//==========================================================================
	//  Constructor
	//==========================================================================
	public function SpecialFinalBuildItemRender()
	{
		super();
	}
	//==========================================================================
	//  Properties
	//==========================================================================
	[Inject]
	public var playerModel:PlayerModel;
	
	protected function get currentItemNum():Number
	{
		return playerModel.getItemNum(resource.key, resource.params);
	}
	
	protected function get requireItemNum():Number
	{
		return INumbericItemValue(resource).num;	
	}
	
	public function get isMeetRequired():Boolean
	{
		if (isFriendRequired)
		{
			return !position.isEmpty;
		}
		return currentItemNum >= requireItemNum;
	}
	
	public function get restCashCost():Number
	{
		if (isFriendRequired)
		{
			return position.cashCost;
		}
		
		var rest:Number = requireItemNum - currentItemNum;
		if (rest <= 0) return 0;
		
		var cash:Number = resource.template.cost.cashCost;
		return cash * rest;
	}
	
	public function get buyBtn():ButtonProxy
	{
		return proxys["buyBtn"];
	}
	
	public function get isFriendRequired():Boolean
	{
		return position != null;
	}
	//==========================================================================
	//  Overridden methods: AssetItemRenderProxy
	//==========================================================================		
	protected var resource:INumbericItemValue;
	
	protected var position:FinalBuildFriendCostPositionValue;
	
	protected var _data:*;
	
	override public function set data(value:*):void
	{
		_data = value;
		
		if (data is INumbericItemValue) 
		{
			resource = INumbericItemValue(data);
			
			skin.nameLabel.text = resource.template.name;
			
			icon.load(resource.template.getIconPathByKey("icon"));
			
			updateNumbericItemRequired();
		}
		else
		{
			position = FinalBuildFriendCostPositionValue(data);
			
			skin.nameLabel.text = position.name;			
			
			updateFriendRequired();
		}
		
		skin.nameLabel.height = skin.nameLabel.textHeight + 6;
		
		buyBtn.text = StringUtil.substitute(Language.getText("@final_build_buy_with_cash_button_label"), restCashCost);
		
		var flag:Boolean = isMeetRequired
		skin.fullLabel.visible = flag;
		
		buyBtn.visible = !flag;
		buyBtn.enabled = !flag;
	}
	
	override public function get data():*
	{
		return _data;
	}
	
	override public function set selected(value:Boolean):void
	{
		
	}
	
	override public function get selected():Boolean
	{
		return false;
	}
	
	override protected function get proxyMapping():Array
	{
		return [ 
			{ skin:iconSkin, classRef: IconProxy, id:"icon" },
			{ skin:"buyBtn", classRef: ButtonProxy, id:"buyBtn", onClick: onClickToBuyHandler }
		];
	}
	
	override public function parse(target:MovieClip):void
	{
		super.parse(target);
	}
	//==========================================================================
	//  Protected methods
	//==========================================================================
	protected function updateNumbericItemRequired():void
	{		
		skin.infoLabel.text = currentItemNum + " / " + requireItemNum;
	}
	
	protected function updateFriendRequired():void
	{		
		if (position.isEmpty)
		{
			skin.emptyFriendPic.visible = true;
		}
		else
		{
			skin.emptyFriendPic.visible = false;
			
			skin.infoLabel.text = position.playerID;
			
			icon.load("");
		}
	}
	//==========================================================================
	//  Event handlers
	//==========================================================================
	protected function onClickToBuyHandler():void
	{
		//Todo: 购买
	}
}
}




